<template>
  <div class="user">
    <!-- 头部布局 -->
    <div class="user_top">
      <!-- 头像 -->
      <img :src="headimg" width="70" alt="" />
      <!-- 显示登录用户 -->
      <h3 v-if="ifLogined">{{ username }}</h3>
      <h3 v-else>点击登录</h3>
      <van-icon :name="!ifLogined ? 'arrow' : 'cross'" @click="loginout" />
    </div>
    <!-- 使用Vant1中的组件 -->
    <Ubutton></Ubutton>
    <Uinput></Uinput>
    <!-- 九宫格部分 -->
    <van-grid square :column-num="3">
      <van-grid-item
        v-for="item in gridArr"
        :key="item.id"
        :icon="item.icon"
        :text="item.type"
      />
    </van-grid>
    <!-- 模态框 -->
    <div class="modal" v-show="isshowmodal">
      <!-- 遮罩层 -->
      <div class="modal_bg" @click="isshowmodal = false"></div>
      <div class="modal_content">
        <!-- 表单 -->
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </div>
    
  </div>
</template>
<script>
// 引入登录接口
import { loginFn } from "@/utils/http";
// 引入头像图片
import headimg from "../assets/images/touxiang.png";
import Ubutton from '../components/Ubutton.vue';
import Uinput from '../components/Uinput.vue';
export default {
  name: "User",
  props: [""],
  data() {
    return {
      value: "",
      ifLogined: false, //定义一个登陆状态
      username: "", //登录用户名
      password: "", //密码
      isshowmodal: false, //模态框是否显示
      headimg: headimg, //头像
      gridArr: [
        // grid数组
        { id: 0, icon: "label-o", type: "我的订单" },
        { id: 1, icon: "bill-o", type: "优惠券" },
        { id: 2, icon: "goods-collect-o", type: "礼品卡" },
        { id: 3, icon: "location-o", type: "我的收藏" },
        { id: 4, icon: "flag-o", type: "我的足迹" },
        { id: 5, icon: "contact", type: "会员福利" },
        { id: 6, icon: "aim", type: "地址管理" },
        { id: 7, icon: "warn-o", type: "账号安全" },
        { id: 8, icon: "service-o", type: "联系客服" },
        { id: 9, icon: "question-o", type: "帮助中心" },
        { id: 10, icon: "smile-comment-o", type: "意见反馈" },
      ],
    };
  },
  created() {
    // 当切换到我的页面,重新触发创建组件实例的生命周期函数
    // console.log('23333');
    // 根据本地的存储用户的信息,判断当前是否登录,如果本地存储中有用户信息,说明已经登陆,否则未登录
    const userinfo = JSON.parse(localStorage.getItem("userinfo"));
    if (userinfo) {
      // 说明已经登录
      this.ifLogined = true;
      this.username = userinfo.username;
    } else {
      // 未登录
    }
  },
  components: {Ubutton, Uinput},
  methods: {
    onSubmit(values) {
      // console.log("submit", values);
      loginFn({ username: values["用户名"], pwd: values["密码"] }).then(
        (res) => {
          // console.log(res);
          if (res.data.errno == 0) {
            // 登录成功
            // 01 修改登录状态
            this.ifLogined = true;
            // 02 将后端返回的token 字段存到本地
            localStorage.setItem("token", res.data.data.token);
            // 03 将后端返回的用户信息,存到本地
            localStorage.setItem(
              "userinfo",
              JSON.stringify(res.data.data.userInfo)
            );
            // 04 隐藏模态框
            this.isshowmodal = false;
            // 05 将用户信息显示到页面中
            this.username = res.data.data.userInfo.username;
            // this.headimg = res.data.data.userInfo.avatar
          } else {
            // 登陆失败
          }
        }
      ).catch((e) => {});
    },
    // 点击登录
    loginout() {
      if (this.ifLogined) {
        // 当为登陆时退出登录
        console.log(this);
        // vant插件,前提是获取到this
        this.$dialog
          .confirm({
            title: "提示",
            message: "确定要退出码",
          })
          .then(() => {
            // 点击确认,表示要确定退出( 退出不需要调用接口,而是修改状态)
            // 01: 将本地储存的用户信息清除掉
            localStorage.removeItem("userinfo");
            // 清空token值
            localStorage.removeItem("token")
            // 登录状态修改
            this.ifLogined = false;
          })
          .catch(() => {
            // on cancel
          });
      } else {
        // 当未登录是,显示模态框
        // 未登录时候,登录框内容为孔
        this.username = "";
        this.password = "";
        this.isshowmodal = true;
      }
    },
  },
};
</script>
<style lang='less' scoped>
.user {
  .user_top {
    background: #333;
    color: #fff;
    padding: 20px 10px;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      border-radius: 50%;
      margin-right: 10px;
    }
    h3 {
      flex: 1;
      font-size: 16px;
    }
  }
  .modal {
    width: 100%;
    height: 100%;
    position: fixed;
    z-index: 99;
    left: 0;
    top: 0;
    .modal_bg {
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.5);
    }
    .modal_content {
      width: 90%;
      height: 200px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 40px;
    }
  }
}
</style>